<?php

namespace common\models\freight;

use Yii;
use common\helpers\ArrayHelper;
use common\models\base\BaseDistrict;

/**
 * This is the model class for table "{{%freight_mode}}".
 *
 * @property int $id
 * @property string $template_id 隶属模版ID
 * @property int $type 货运方式
 * @property string $parent_id
 * @property int $quantity 默认数量
 * @property string $price 默认运费
 * @property int $increase_quantity 每增加数量
 * @property string $increase_price 增加运费
 */
class FreightMode extends \common\db\ActiveRecord {

    use \common\traits\Constants;

    //
    const TYPE_EXPRESS = 0;
    const TYPE_EMS = 1;
    const TYPE_SURFACE = 2;
    const TYPE_LOGISTICS = 3;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%freight_mode}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['template_id', 'type', 'parent_id', 'quantity', 'increase_quantity'], 'integer'],
            [['price', 'increase_price'], 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'template_id' => '隶属模版ID',
            'type' => '货运方式',
            'parent_id' => '默认计价ID',
            'quantity' => '首',
            'price' => '运费',
            'increase_quantity' => '续',
            'increase_price' => '增加运费',
        ];
    }

    public function getTemplate() {
        return $this->hasOne(FreightTemplate::className(), ['id' => 'template_id']);
    }

    public function getAreas() {
        return $this->hasMany(FreightModeArea::className(), ['mode_id' => 'id']);
    }

    public function getAreaCodes() {
        return ArrayHelper::getColumn($this->areas, 'code');
    }

    public function getAreaNames() {
        return BaseDistrict::getUnifinedName($this->areaCodes);
    }

}
